/// 运动状态机枚举
/// 定义了机器人的各种运动状态及其对应的错误码
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SportState {
    /// 灵动模式 (error_code: 100)
    Agile,
    /// 阻尼模式 (error_code: 1001)
    Damping,
    /// 站立锁定 (error_code: 1002)
    StandLock,
    /// 蹲下 (error_code: 1004/2006)
    Squat,
    /// 打招呼/伸懒腰/舞蹈/拜年/比心/开心 (error_code: 1006)
    Gesture,
    /// 坐下 (error_code: 1007)
    Sit,
    /// 前跳 (error_code: 1008)
    ForwardJump,
    /// 扑人 (error_code: 1009)
    Pounce,
    /// 平衡站立 (error_code: 1013)
    BalanceStand,
    /// 常规行走 (error_code: 1015)
    NormalWalk,
    /// 常规跑步 (error_code: 1016)
    NormalRun,
    /// 常规续航 (error_code: 1017)
    NormalEndurance,
    /// 摆姿势 (error_code: 1091)
    Pose,
    /// 闪避 (error_code: 2007)
    Dodge,
    /// 并腿跑 (error_code: 2008)
    LegsTogether,
    /// 跳跃跑 (error_code: 2009)
    JumpRun,
    /// 经典 (error_code: 2010)
    Classic,
    /// 倒立 (error_code: 2011)
    Handstand,
    /// 前空翻 (error_code: 2012)
    ForwardFlip,
    /// 后空翻 (error_code: 2013)
    BackFlip,
    /// 左空翻 (error_code: 2014)
    LeftFlip,
    /// 交叉步 (error_code: 2016)
    CrossStep,
    /// 直立 (error_code: 2017)
    Upright,
    /// 牵引 (error_code: 2019)
    Traction,
}

impl SportState {
    /// 从错误码转换为状态
    pub fn from_error_code(error_code: i32) -> Option<Self> {
        match error_code {
            100 => Some(Self::Agile),
            1001 => Some(Self::Damping),
            1002 => Some(Self::StandLock),
            1004 | 2006 => Some(Self::Squat), // 处理多码对应同一状态的情况
            1006 => Some(Self::Gesture),
            1007 => Some(Self::Sit),
            1008 => Some(Self::ForwardJump),
            1009 => Some(Self::Pounce),
            1013 => Some(Self::BalanceStand),
            1015 => Some(Self::NormalWalk),
            1016 => Some(Self::NormalRun),
            1017 => Some(Self::NormalEndurance),
            1091 => Some(Self::Pose),
            2007 => Some(Self::Dodge),
            2008 => Some(Self::LegsTogether),
            2009 => Some(Self::JumpRun),
            2010 => Some(Self::Classic),
            2011 => Some(Self::Handstand),
            2012 => Some(Self::ForwardFlip),
            2013 => Some(Self::BackFlip),
            2014 => Some(Self::LeftFlip),
            2016 => Some(Self::CrossStep),
            2017 => Some(Self::Upright),
            2019 => Some(Self::Traction),
            _ => None,
        }
    }

    /// 获取状态对应的主要错误码
    /// 对于有多个错误码的状态，返回较小的那个
    pub fn to_error_code(&self) -> i32 {
        match self {
            Self::Agile => 100,
            Self::Damping => 1001,
            Self::StandLock => 1002,
            Self::Squat => 1004, // 返回较小的错误码
            Self::Gesture => 1006,
            Self::Sit => 1007,
            Self::ForwardJump => 1008,
            Self::Pounce => 1009,
            Self::BalanceStand => 1013,
            Self::NormalWalk => 1015,
            Self::NormalRun => 1016,
            Self::NormalEndurance => 1017,
            Self::Pose => 1091,
            Self::Dodge => 2007,
            Self::LegsTogether => 2008,
            Self::JumpRun => 2009,
            Self::Classic => 2010,
            Self::Handstand => 2011,
            Self::ForwardFlip => 2012,
            Self::BackFlip => 2013,
            Self::LeftFlip => 2014,
            Self::CrossStep => 2016,
            Self::Upright => 2017,
            Self::Traction => 2019,
        }
    }

    /// 获取状态的所有错误码
    /// 对于有多个错误码的状态，返回所有可能的错误码
    pub fn all_error_codes(&self) -> Vec<i32> {
        match self {
            Self::Squat => vec![1004, 2006], // 蹲下状态有两个错误码
            _ => vec![self.to_error_code()],
        }
    }

    /// 获取状态的中文名称
    pub fn name(&self) -> &'static str {
        match self {
            Self::Agile => "灵动",
            Self::Damping => "阻尼",
            Self::StandLock => "站立锁定",
            Self::Squat => "蹲下",
            Self::Gesture => "打招呼/伸懒腰/舞蹈/拜年/比心/开心",
            Self::Sit => "坐下",
            Self::ForwardJump => "前跳",
            Self::Pounce => "扑人",
            Self::BalanceStand => "平衡站立",
            Self::NormalWalk => "常规行走",
            Self::NormalRun => "常规跑步",
            Self::NormalEndurance => "常规续航",
            Self::Pose => "摆姿势",
            Self::Dodge => "闪避",
            Self::LegsTogether => "并腿跑",
            Self::JumpRun => "跳跃跑",
            Self::Classic => "经典",
            Self::Handstand => "倒立",
            Self::ForwardFlip => "前空翻",
            Self::BackFlip => "后空翻",
            Self::LeftFlip => "左空翻",
            Self::CrossStep => "交叉步",
            Self::Upright => "直立",
            Self::Traction => "牵引",
        }
    }

    /// 检查错误码是否对应此状态
    pub fn matches_error_code(&self, error_code: i32) -> bool {
        self.all_error_codes().contains(&error_code)
    }
}